﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aidbrella.Web.Common;

namespace Aidbrella.Web.Controllers
{
    public partial class FeedController : BaseController
    {
        public class UpdateData
        {
            public string Text { get; set; }
            public long Timestamp { get; set; }
            public string DisplaybleTime { get; set; }
            public string Name { get; set; }
        }

        public static List<UpdateData> _updates = new List<UpdateData> { new UpdateData { DisplaybleTime = "Yesterday", Name = "Jorma", Text = "Vein puita Nigeriaan", Timestamp = DateTime.Today.Ticks }};

        //
        // GET: /Feed/

        public virtual ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public virtual JsonResult GetProjectFeed(int projectId, int userId, long timestamp)
        {   
            List<UpdateData> updatesToSend = new List<UpdateData>();
            if (timestamp <= 0)
            {
                // we want "all" updates
                updatesToSend = _updates.Take(Constants.feed_load_max_count).ToList();
            }
            else
            {
                // get new updates
                updatesToSend = _updates.Where(o => o.Timestamp > timestamp).ToList();
            }

            return Json(new { isAppend = true, updates = updatesToSend.ToArray() }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public virtual JsonResult AddProjectUpdate(int projectId, int userId, string updateText)
        {
            _updates.Add(new UpdateData { DisplaybleTime = DateTime.Now.ToShortDateString(), Name = userId.ToString(), Text = updateText, Timestamp = DateTime.Now.Ticks});
            return Json(null);
        }
    }
}
